package com.zyx.concurrentdemo.mytest.c04;

import lombok.extern.slf4j.Slf4j;

import static com.zyx.concurrentdemo.c02basic.util.Sleeper.sleep;

/**
 * @author Yaxi.Zhang
 * @since 2021/10/14 16:41
 */
@Slf4j(topic = "c.WaitForWork")
public class WaitForWork {

    private static final Object LOCK = new Object();
    private static boolean hasCigarette;
    private static boolean hasTakeout;

    public static void main(String[] args) {

        new Thread(() -> {
            log.debug("有烟没?[{}]", hasCigarette);
            synchronized (LOCK) {
                while (!hasCigarette) {
                    try {
                        LOCK.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.debug("有烟没？[{}]", hasCigarette);
                if (hasCigarette) {
                    log.debug("开始工作了");
                }
            }
        }, "小南").start();


        for (int i = 0; i < 6; i++) {
            new Thread(() -> {
                synchronized (LOCK) {
                    log.debug("开始工作了");
                }
            }, "其他人").start();
        }

        sleep(3);

        new Thread(() -> {
            synchronized (LOCK) {
                log.debug("送烟来了");
                hasCigarette = true;
                LOCK.notifyAll();
            }
        }, "小陈").start();


    }
}
